//
//  WTGiftShowView.m
//  66TV
//
//  Created by ben on 16/8/3.
//  Copyright © 2016年 shifeng. All rights reserved.
//

#import "WTGiftShowView.h"
#import "UIColor+WTExtend.h"
#import "Masonry.h"
#import "UIImageView+WebCache.h"
#import "UIImage+WTExtend.h"

@interface WTGiftShowView ()
@property (nonatomic, strong) UIView*       bgView;
@property (nonatomic, strong) UIImageView*  avatarImageView;
@property (nonatomic, strong) UILabel*      giftContentLabel;
@property (nonatomic, strong) UIImageView*  giftImageView;
@property (nonatomic, copy) WTGiftShowViewCompleteBlock giftShowCompleteBlock;

@property (nonatomic, assign) NSInteger totlalGiftCount;
@end


@implementation WTGiftShowView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setup];
    }
    return self;
}

#pragma mark - 
- (void)setup{
    
    self.bgView = ({
        UIView* view = [[UIView alloc] init];
        view.layer.cornerRadius = 13;
        view.layer.masksToBounds = YES;
        view.backgroundColor = [UIColor colorWithWhite:0 alpha:0.4];
        [self addSubview:view];
        view;
    });
    
    self.avatarImageView = ({
        UIImageView* view = [[UIImageView alloc] init];
        view.layer.cornerRadius = 13;
        view.layer.masksToBounds = YES;
        [self.bgView addSubview:view];
        view;
    });
    
    
    self.giftContentLabel = ({
        UILabel* label = [[UILabel alloc] init];
        [self.bgView addSubview:label];
        label;
    });
    
    self.giftImageView = ({
        UIImageView* view = [[UIImageView alloc] init];
        view.contentMode = UIViewContentModeScaleToFill;
        [self.bgView addSubview:view];
        view;
    });
    
    self.giftNumberLabel = ({
        WTShakeLabel* label = [[WTShakeLabel alloc] init];
        label.text = @"X6";
        label.font = [UIFont boldSystemFontOfSize:15];
        label.textColor = [UIColor hexChangeFloat:@"FF6969"];
        [self addSubview:label];
        label;
    });
    
    [self layout];
    
}

#pragma mark - 
- (void)layout{
    
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.and.bottom.mas_equalTo(0);
        make.leading.mas_equalTo(10);
        make.trailing.mas_equalTo(self.giftImageView.mas_trailing).offset(5);
    }];
    
    [self.avatarImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(0);
        make.width.and.height.mas_equalTo(27);
        make.centerY.mas_equalTo(self.mas_centerY);
    }];
    
    [self.giftContentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.avatarImageView.mas_trailing).offset(8);
        make.centerY.mas_equalTo(self.avatarImageView.mas_centerY);
    }];
    
    [self.giftImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.and.height.mas_equalTo(20);
        make.centerY.mas_equalTo(self.mas_centerY);
        make.leading.mas_equalTo(self.giftContentLabel.mas_trailing).offset(8);
    }];
    
    [self.giftNumberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.bgView.mas_trailing).offset(8);
        make.centerY.mas_equalTo(self.bgView.mas_centerY);
    }];

    
    
    NSString* text = [NSString stringWithFormat:@"%@ %@",@"Amini",@"送了一个巧克力"];
    NSDictionary* attribute = @{NSFontAttributeName:[UIFont systemFontOfSize:14],NSForegroundColorAttributeName:[UIColor themeColor]};
    NSMutableAttributedString* string = [[NSMutableAttributedString alloc] initWithString:text attributes:attribute];
    [string addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:[text rangeOfString:@"Amini"]];
    self.giftContentLabel.attributedText = string;
}


#pragma mark - 
- (void)animateWithCompleteBlock:(WTGiftShowViewCompleteBlock)completeBlock{
    
    [UIView animateWithDuration:0.3 animations:^{
        self.frame = CGRectMake(0, self.frame.origin.y, self.frame.size.width, self.frame.size.height);
    } completion:^(BOOL finished) {
        [self shakeNumberLabel];
    }];
    self.giftShowCompleteBlock = completeBlock;
}

- (void)shakeNumberLabel{
    
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(hidePresendView) object:nil];//可以取消成功。
    [self performSelector:@selector(hidePresendView) withObject:nil afterDelay:2];
    self.giftNumberLabel.text = [NSString stringWithFormat:@"X %zd",self.totlalGiftCount];
    [self.giftNumberLabel startAnimWithDuration:0.3];
}

- (void)hidePresendView
{
    [UIView animateWithDuration:0.30 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        self.frame = CGRectMake(0, self.frame.origin.y - 10, self.frame.size.width, self.frame.size.height);
        self.alpha = 0;
    } completion:^(BOOL finished) {
        if (self.giftShowCompleteBlock) {
            self.giftShowCompleteBlock(finished,self.totlalGiftCount);
        }
        [self reset];
        [self removeFromSuperview];
    }];
}

- (void)reset {
    
    self.frame = CGRectZero;
    self.alpha = 1;
    self.totlalGiftCount = 0;
    self.giftNumberLabel.text = @"";
}


- (void)setGiftModel:(WTChatRoomGiftModel *)giftModel{
    
    self.totlalGiftCount += giftModel.gift_count;
    
    [self.avatarImageView sd_setImageWithURL:[NSURL URLWithString:giftModel.gift_sender_avatar] placeholderImage:[UIImage wt_imageWithName:@"wt_avatar"]];
    [self.giftImageView sd_setImageWithURL:[NSURL URLWithString:giftModel.gift_image_url] placeholderImage:[UIImage wt_imageWithName:@"wt_avatar"]];
    
    NSString* text = [NSString stringWithFormat:@"%@ %@%@",giftModel.gift_sender_name,@"送了一个",giftModel.gift_name];
    NSDictionary* attribute = @{NSFontAttributeName:[UIFont systemFontOfSize:14],NSForegroundColorAttributeName:[UIColor themeColor]};
    NSMutableAttributedString* string = [[NSMutableAttributedString alloc] initWithString:text attributes:attribute];
    [string addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:[text rangeOfString:giftModel.gift_name]];
    self.giftContentLabel.attributedText = string;
}

@end
